﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package reprise.ui.layoutmanagers {
    import reprise.ui.*;
    import reprise.css.*;
    import flash.display.*;
    import flash.geom.*;

    public class CSSBoxModelLayoutManager implements ILayoutManager {

        private var m_displayStack:Array;

        protected function applyVerticalPositionsInLineBox(_arg1:int, _arg2:int, _arg3:Array):void{
            var _local5:UIComponent;
            var _local4:int = _arg3.length;
            while (_local4--) {
                _local5 = _arg3[_local4];
                switch (_local5.style.verticalAlign){
                    case "middle":
                        _local5.y = (_arg1 + Math.round(((_arg2 / 2) - (((_local5.outerHeight + _local5.style.collapsedMarginTop) + _local5.style.collapsedMarginBottom) / 2))));
                        break;
                    case "bottom":
                    case "baseline":
                        _local5.y = (_arg1 + Math.round((_arg2 - ((_local5.outerHeight + _local5.style.collapsedMarginTop) + _local5.style.collapsedMarginBottom))));
                        break;
                };
            };
        }
        public function applyAbsolutePositions(_arg1:UIComponent, _arg2:Array):void{
            var _local5:UIComponent;
            var _local6:ComputedStyles;
            var _local7:Point;
            var _local3:int = _arg2.length;
            var _local4:int;
            while (_local4 < _local3) {
                _local5 = (_arg2[_local4] as UIComponent);
                if (((!(_local5)) || (!(_local5.isRendered())))){
                } else {
                    _local6 = _local5.style;
                    if (((!(_local5.positionInFlow)) && (!(_local6.float)))){
                        _local7 = _local5.getPositionRelativeToContext(_local5.containingBlock);
                        _local5.getPositionRelativeToContext(_local5.containingBlock).x = (_local7.x - _local5.x);
                        _local7.y = (_local7.y - _local5.y);
                        if (!_local5.autoFlags.left){
                            if (((((!(_local5.autoFlags.right)) && (_local5.autoFlags.marginLeft))) && (_local5.autoFlags.marginRight))){
                                _local5.x = (_local6.left + Math.round(((((_local5.containingBlock.paddingBoxWidth - _local6.right) - _local6.left) / 2) - (_local5.borderBoxWidth / 2))));
                            } else {
                                _local5.x = ((_local6.left + _local6.marginLeft) - _local7.x);
                            };
                        } else {
                            if (!_local5.autoFlags.right){
                                _local5.x = ((((_local5.containingBlock.paddingBoxWidth - _local5.borderBoxWidth) - _local6.right) - _local6.marginRight) - _local7.x);
                            };
                        };
                        if (!_local5.autoFlags.top){
                            if (((((!(_local5.autoFlags.bottom)) && (_local5.autoFlags.marginTop))) && (_local5.autoFlags.marginBottom))){
                                _local5.y = (_local6.top + Math.round(((((_local5.containingBlock.paddingBoxHeight - _local6.bottom) - _local6.top) / 2) - (_local5.borderBoxHeight / 2))));
                            } else {
                                _local5.y = ((_local6.top + _local6.marginTop) - _local7.y);
                            };
                        } else {
                            if (!_local5.autoFlags.bottom){
                                _local5.y = ((((_local5.containingBlock.paddingBoxHeight - _local5.borderBoxHeight) - _local6.bottom) - _local6.marginBottom) - _local7.y);
                            };
                        };
                    };
                };
                _local4++;
            };
        }
        public function applyFlowPositions(_arg1:UIComponent, _arg2:Array):void{
            var collapsibleMargin:int;
            var totalAvailableWidth:int;
            var currentLineBoxTop:int;
            var currentLineBoxHeight:int;
            var initialLineBoxLeftBoundary:int;
            var i:int;
            var child:UIComponent;
            var childStyle:ComputedStyles;
            var depthStackEntry:Object;
            var childWidth:int;
            var childVAlign:String;
            var childMarginTop:int;
            var collapsedMargin:int;
            var element = _arg1;
            var children = _arg2;
            var closeLineBox:Function = function ():void{
                if (currentLineBoxHeight){
                    currentLineBoxTop = (currentLineBoxTop + (currentLineBoxHeight + collapsibleMargin));
                    collapsibleMargin = 0;
                    currentLineBoxHeight = 0;
                    currentLineBoxLeftBoundary = initialLineBoxLeftBoundary;
                    currentLineBoxRightBoundary = (totalAvailableWidth + initialLineBoxLeftBoundary);
                    currentLineBoxChildren = [];
                };
            };
            var childCount:int = children.length;
            if (!childCount){
                element.style.intrinsicHeight = 0;
                element.style.intrinsicWidth = 0;
                return;
            };
            element.style.collapsedMarginTop = element.style.marginTop;
            element.style.collapsedMarginBottom = element.style.marginBottom;
            m_displayStack = [];
            var elementStyle:ComputedStyles = element.style;
            var widestChildWidth:int;
            collapsibleMargin = 0;
            var topMarginCollapsible:Boolean = ((((!(elementStyle.borderTopWidth)) && (!(elementStyle.paddingTop)))) && (element.positionInFlow));
            if (topMarginCollapsible){
                collapsibleMargin = elementStyle.marginTop;
            };
            totalAvailableWidth = element.innerWidth();
            currentLineBoxTop = elementStyle.paddingTop;
            currentLineBoxHeight = 0;
            initialLineBoxLeftBoundary = elementStyle.paddingLeft;
            var currentLineBoxLeftBoundary:int = initialLineBoxLeftBoundary;
            var currentLineBoxRightBoundary:int = (totalAvailableWidth + initialLineBoxLeftBoundary);
            var currentLineBoxChildren:Array = [];
            i = 0;
            while (i < childCount) {
                child = (children[i] as UIComponent);
                if (((!(child)) || (!(child.isRendered())))){
                } else {
                    childStyle = child.style;
                    if (childStyle.float){
                        if (collapsibleMargin){
                            currentLineBoxTop = (currentLineBoxTop + collapsibleMargin);
                            collapsibleMargin = 0;
                        };
                        childWidth = ((child.borderBoxWidth + childStyle.marginLeft) + childStyle.marginRight);
                        if (childWidth > (currentLineBoxRightBoundary - currentLineBoxLeftBoundary)){
                            if (currentLineBoxChildren.length){
                                applyVerticalPositionsInLineBox(currentLineBoxTop, currentLineBoxHeight, currentLineBoxChildren);
                            };
                            closeLineBox();
                        };
                        if (childStyle.float == "left"){
                            child.x = (currentLineBoxLeftBoundary + childStyle.marginLeft);
                            currentLineBoxLeftBoundary = ((child.x + child.outerWidth) + childStyle.marginRight);
                        } else {
                            if (childStyle.float == "right"){
                                child.x = ((currentLineBoxRightBoundary - child.outerWidth) - childStyle.marginRight);
                                currentLineBoxRightBoundary = (child.x - childStyle.marginLeft);
                            };
                        };
                        currentLineBoxHeight = Math.max(currentLineBoxHeight, ((child.outerHeight + childStyle.collapsedMarginTop) + childStyle.collapsedMarginBottom));
                        childVAlign = ((childStyle.verticalAlign) || ("top"));
                        if (childVAlign != "top"){
                            currentLineBoxChildren.push(child);
                        };
                    } else {
                        if (((child.positionInFlow) || (((child.autoFlags.left) && (child.autoFlags.right))))){
                            if (child.autoFlags.marginLeft){
                                if (child.autoFlags.marginRight){
                                    child.x = (elementStyle.paddingLeft + Math.round(((totalAvailableWidth / 2) - (child.outerWidth / 2))));
                                } else {
                                    child.x = (((elementStyle.paddingLeft + totalAvailableWidth) - child.outerWidth) - childStyle.marginRight);
                                };
                            } else {
                                child.x = (childStyle.marginLeft + elementStyle.paddingLeft);
                            };
                        };
                    };
                    if (((child.positionInFlow) || (childStyle.float))){
                        widestChildWidth = Math.max(((child.x + child.outerWidth) + childStyle.marginRight), widestChildWidth);
                    };
                    if (child.positionInFlow){
                        closeLineBox();
                        childMarginTop = childStyle.collapsedMarginTop;
                        if ((((collapsibleMargin >= 0)) && ((childMarginTop >= 0)))){
                            collapsedMargin = Math.max(collapsibleMargin, childMarginTop);
                        } else {
                            if ((((((collapsibleMargin >= 0)) && ((childMarginTop < 0)))) || ((((collapsibleMargin < 0)) && ((childMarginTop >= 0)))))){
                                collapsedMargin = (collapsibleMargin + childMarginTop);
                            } else {
                                collapsedMargin = Math.min(collapsibleMargin, childMarginTop);
                            };
                        };
                        if (topMarginCollapsible){
                            elementStyle.collapsedMarginTop = collapsedMargin;
                            collapsedMargin = 0;
                            topMarginCollapsible = false;
                        };
                        child.y = (currentLineBoxTop + collapsedMargin);
                        if (!child.outerHeight){
                            collapsibleMargin = Math.max(collapsedMargin, childStyle.collapsedMarginBottom);
                        } else {
                            collapsibleMargin = childStyle.collapsedMarginBottom;
                            topMarginCollapsible = false;
                            currentLineBoxTop = (child.y + child.outerHeight);
                        };
                    } else {
                        if (((childStyle.float) || (((child.autoFlags.top) && (child.autoFlags.bottom))))){
                            if (!childStyle.float){
                                closeLineBox();
                            };
                            child.y = ((currentLineBoxTop + (topMarginCollapsible) ? 0 : collapsibleMargin) + childStyle.marginTop);
                        };
                    };
                    depthStackEntry = {element:child, index:i, zIndex:((childStyle.zIndex) || (0))};
                    (((depthStackEntry.zIndex > 0)) && (depthStackEntry.zIndex++));
                    m_displayStack.push(depthStackEntry);
                };
                i = (i + 1);
            };
            if (((((((collapsibleMargin) && (!(elementStyle.borderBottomWidth)))) && (!(elementStyle.paddingBottom)))) && (element.positionInFlow))){
                elementStyle.collapsedMarginBottom = Math.max(elementStyle.marginBottom, collapsibleMargin);
                collapsibleMargin = 0;
            };
            if (currentLineBoxChildren.length){
                applyVerticalPositionsInLineBox(currentLineBoxTop, currentLineBoxHeight, currentLineBoxChildren);
            };
            elementStyle.intrinsicHeight = Math.max((((currentLineBoxTop + currentLineBoxHeight) + collapsibleMargin) - elementStyle.paddingTop), 0);
            elementStyle.intrinsicWidth = Math.max((widestChildWidth - elementStyle.paddingLeft), 0);
        }
        public function applyDepthSorting(_arg1:DisplayObjectContainer, _arg2:DisplayObjectContainer):void{
            var _local6:DisplayObject;
            if (((!(m_displayStack)) || (!(m_displayStack.length)))){
                return;
            };
            m_displayStack.sortOn(["zIndex", "index"], Array.NUMERIC);
            var _local3:int;
            var _local4:int;
            var _local5:int;
            while (_local5 < m_displayStack.length) {
                _local6 = m_displayStack[_local5].element;
				
				
				if(_local6.parent){
					//_local6.parent.setChildIndex(_local6,_local5);
					
					
					if(_local6.parent==_arg1){
						var _temp1 = _local3;
						_local3 = (_local3 + 1);
						_local6.parent.setChildIndex(_local6,_temp1);
					}else{
						var _temp2 = _local4;
						_local4 = (_local4 + 1);
						_local6.parent.setChildIndex(_local6,_temp2);
					}
				}
				
//                ((_local6.parent) && (_local6.parent.setChildIndex(_local6, ((_local6.parent == _arg1)) ? var _temp1 = _local3;
//_local3 = (_local3 + 1);
//_temp1 : var _temp2 = _local4;
//_local4 = (_local4 + 1);
//_temp2)));
                _local5++;
            };
            m_displayStack = null;
        }

    }
}//package reprise.ui.layoutmanagers 
